package pojo;

import utils.JDBCUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Student {
    private String id;
    private String name;
    private String password;
    private String sex;
    private String major;
    private int age;

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", sex='" + sex + '\'' +
                ", major='" + major + '\'' +
                ", age=" + age +
                '}';
    }

    public Student() {
    }

    public Student(String id, String name, String password, String sex, String major, int age) {
        super();
        this.id = id;
        this.name = name;
        this.password = password;
        this.sex = sex;
        this.major = major;
        this.age = age;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    //添加
    public static void saveStudent(Student student) {
        String sql = "insert into student(id, name, password,sex,major,age) values(?,?,?,?,?,?)";
        try {
            PreparedStatement pstmt = JDBCUtils.getconnection().prepareStatement(sql);
            pstmt.setString(1,student.getId());
            System.out.println(student.getId());
            pstmt.setString(2,student.getName());
            System.out.println(student.getName());
            pstmt.setString(3,student.getPassword());
            System.out.println(student.getPassword());
            pstmt.setString(4,student.getSex());
            System.out.println(student.getSex());
            pstmt.setString(5,student.getMajor());
            System.out.println(student.getMajor());
            pstmt.setInt(6,student.getAge());
            System.out.println(student.getAge());
            int rs = pstmt.executeUpdate();
            System.out.println("影响行数：" + rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //显示所有学生信息
    public List<Student> findAllStudents(){
        List<Student> stuList = new ArrayList<Student>();
        String sql = "select * from student";
        PreparedStatement pstmt = null;
        try {
            pstmt = JDBCUtils.getconnection().prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()) {
                String id = rs.getString("id");
                String name = rs.getString("name");
                String password = rs.getString("password");
                String sex = rs.getString("sex");
                String major = rs.getString("major");
                int age = rs.getInt("age");
                Student stu = new Student(id,name,password,sex,major,age);
                stuList.add(stu);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.free();
        }
        return stuList;
    }
}
